package com.example.jixeishi1.controller;

import com.example.jixeishi1.entity.Configure_price;
import com.example.jixeishi1.entity.Order;
import com.example.jixeishi1.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.List;

@Controller
public class GoodsController {

    @Autowired
    GoodsService goodsService;
    @RequestMapping("/goodsPage")
    public String goodsPage(ModelMap map,String uid,String gid) throws SQLException {
int uid1=Integer.parseInt(uid);
        int gid1=Integer.parseInt(gid);
        System.out.println("购物Page");
        List goods=goodsService.getGoods(gid1);
        map.put("goods",goods);

        System.out.println("购物详情");
        List details=goodsService.getDetails(gid1);
        map.put("details",details);

        System.out.println("配置价格");
        List cf_p=goodsService.getCf_p(gid1);
        System.out.println(cf_p);
        map.put("cfp",cf_p);

        System.out.println("用户评价");
        List evaluate=goodsService.getEvaluate(gid1);
        System.out.println(evaluate);
        map.put("evaluate",evaluate);

        System.out.println("地址");
        List address=goodsService.getAddress(uid1);
        map.put("address",address);
        System.out.println(address);




        List hotgoods=goodsService.getHotGoods();
        map.put("hotgoods",hotgoods);
        map.put("uid",uid1);
        map.put("gid",gid1);

        return "goods";
    }

    @ResponseBody
    @RequestMapping("goodsPrice")
    public List<Configure_price> goodsPrice(ModelMap map, @RequestParam(defaultValue = "1")int goodsid) throws SQLException {
        System.out.println("get商品价格");
        List<Configure_price> property=goodsService.getProperty(goodsid);
        map.put("property",property);
        System.out.println("商品属性");
        List pr=goodsService.getProperty(goodsid);
        map.put("psx",pr);
        return property;
    }


    @RequestMapping("orderAdd")
    public String addOrder( ModelMap map,String gimg, String gname, String gprice, String gaddress, String gnum,String uid,String gid)throws SQLException{

        System.out.println("订单添加");
        System.out.println(gimg);
        System.out.println(gname);
        System.out.println(gprice);
        System.out.println(gaddress);
        System.out.println(gnum);
        int max=100000000,min=90000000;
        int ran = (int) (Math.random()*(max-min)+min);
        String ddh=""+ran;
        int uid1=Integer.parseInt(uid);
        int gid1=Integer.parseInt(gid);
        Order sum=new Order();
        int gaddress1=Integer.parseInt(gaddress) ;
        sum.setAddressid(gaddress1);
        double gprice1=Double.parseDouble(gprice);
        sum.setPrices(gprice1);
        sum.setOrderno(ddh);
        int gnum1=Integer.parseInt(gnum);
        sum.setShippingid(gaddress1);
        sum.setPayment(gprice1*gnum1);
        sum.setPostage(0);
        sum.setStatus(10);
        sum.setUserid(uid1);
        sum.setProductGoods(gname);
        sum.setQuantity(gnum1);
        sum.setPictureImg(gimg);
        goodsService.addOrder(sum);



        System.out.println("购物Page");
        List goods=goodsService.getGoods(gid1);
        map.put("goods",goods);

        System.out.println("购物详情");
        List details=goodsService.getDetails(gid1);
        map.put("details",details);

        System.out.println("配置价格");
        List cf_p=goodsService.getCf_p(gid1);
        System.out.println(cf_p);
        map.put("cfp",cf_p);

        System.out.println("用户评价");
        List evaluate=goodsService.getEvaluate(gid1);
        System.out.println(evaluate);
        map.put("evaluate",evaluate);

        System.out.println("地址");
        List address=goodsService.getAddress(uid1);
        map.put("address",address);
        System.out.println(address);

map.put("jiag",gprice1);
map.put("spm",gname);
map.put("pd",1);

//        List hotgoods=goodsService.getHotGoods();
//        map.put("hotgoods",hotgoods);
//        map.put("uid",uid1);
        return "ok";
    }

}
